<?php

namespace Biz\Play;

use Base\Exception\NoRecordException;

class Result extends \Base\BaseBiz
{

    //添加比赛结果信息
    public static function insert($_orm)
    {
        $rs = array('isok' => false,'msg'=>'');
        try {
            $result_orm = new \Orm\PlayResult\PlayResultClass();
            $result_orm['play_type'] = $_orm['play_type']??'';
            $result_orm['play_showing_id'] = $_orm['play_showing_id']??'';
            $result_orm['result_setting_id'] = $_orm['result_setting_id']??'';
            $result_orm->insert();
            if($result_orm['id']){
                $rs['isok'] = true;
            }else{
                throw new \Exception('新增数据失败');
            }
        }catch (\Exception $e) {
            $rs['isok'] = false;
            $rs['msg'] = $e->getMessage();
        }
        return $rs;
    }

    /*
     * 获取列表
     */
    public static function getList($where, $now = 1, $size = 0)
    {
        $now = ($now > 0)?$now:1;
        $size = ($size > 0)?$size:\Util\Config::get('max_limit');

        $result_list = new \Orm\PlayResult\PlayResultList();
        if (!empty($where)) {
            $result_list['where'] = $where;
        } else {
            $result_list['where'] = '';
        }
        $result_list['limit'] = $size;
        $result_list['offset'] = ($now - 1) * $size;
        $result_list['order'] = 'id ASC';
        $result_list['key_by'] = 'id';
        $result_list->load();

        $result = array();
        $result['lists'] = $result_list['lists'];
        $result['total'] = $result_list['total'];
        return $result;
    }

    /*
     * 获取指定比赛的结果
     */

    public static function getInfoBySid($sid)
    {
        $result_list = new \Orm\PlayResult\PlayResultList();
        $result_list['where'] = 'play_showing_id = '.$sid;
        $result_list['order'] = 'id ASC';
        $result_list['key_by'] = 'id';
        $result_list->load();

        $result = array(\Biz\Play\Settings::TYPE_A => 0,\Biz\Play\Settings::TYPE_B => 0,\Biz\Play\Settings::TYPE_C => 0);
        if($result_list['lists']) {
            foreach ($result_list['lists'] as $item) {
                if ((int)$item['play_type'] === \Biz\Play\Settings::TYPE_A) {
                    $result[\Biz\Play\Settings::TYPE_A] = $item['result_setting_id'];
                }
                if ((int)$item['play_type'] === \Biz\Play\Settings::TYPE_B) {
                    $result[\Biz\Play\Settings::TYPE_B] = $item['result_setting_id'];
                }
                if ((int)$item['play_type'] === \Biz\Play\Settings::TYPE_C) {
                    $result[\Biz\Play\Settings::TYPE_C] = $item['result_setting_id'];
                }
            }
        }
        return $result;
    }
}
